//
//  ClubBlackListViewController.swift
//  77Club
//
//  Created by Apple on 2024/10/22.
//

import UIKit

class ClubBlackListViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("黑名单").color(.black).font(16.font_semiBold).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubBlackListCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 56.0.Scale
        return tableView
    }()
    private var emptyView = ClubBlackEmptyView()
    
    private var dataSource = [ClubMineBlackModel]()
    private var pageIndex = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        loadData()
        
        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.Leading == backButton.Trailing
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(0)
        emptyView.leading(0).trailing(0).top(.navi_height + 100).height(200)
        emptyView.isHidden = true
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadNewData()
        }
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    

}

extension ClubBlackListViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubBlackListCell", for: indexPath) as! ClubBlackListCell
        cell.model = dataSource[indexPath.row]
        cell.removeBlackBlock = { uid in
            self.removeBlackUser(uid: uid)
        }
        return cell
    }
}

//MARK: data
extension ClubBlackListViewController {

    private func loadData(){
        
        Task {
            let res = await network.request(.blackList(page: pageIndex),type: ClubMineBlackModel.self,isShowError: false)
            tableView.refresh_end()
            if res.isSuccess, let models = res.models {
                self.emptyView.isHidden = models.count > 0
                self.dataSource = models
                self.tableView.reloadData()
            }
        }
    }

    @objc func loadNewData(){
        pageIndex = 1
        loadData()
    }
    
    @objc func loadMoreData(){
        pageIndex += 1
        Task {
            let models = await network.request(.blackList(page: pageIndex),type: ClubMineBlackModel.self,isShowError: false)
            if models.isSuccess, let models = models.models {
                if models.count > 0 {
                    self.dataSource.append(contentsOf: models)
                }else{
                    tableView.refresh_end(true)
                }
                self.tableView.reloadData()
            }
        }
    }

    private func removeBlackUser(uid: String) {
        if let user_id = UserDefaultsManager.getString(forKey: .uid), user_id.count > 0 {
            Task {
                let models = await network.request(.delBlackList(uid: uid),isShowError: false)
                if models.isSuccess {
                    "移除成功".toast_success
                    self.loadNewData()
                }
            }
        }else{
            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
        }
    }
}


class ClubBlackListCell: UITableViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(20.0.Scale).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#17181A").font(15.font_medium).end()
    private let removeButton = c_make_ex(UIButton.self)
        .image("Mine_black_remove").end()

    var model:ClubMineBlackModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.loadImage(url: md.face)
            nameLabel.text = md.pet_name
        }
    }
    
    var removeBlackBlock: ((_ userID: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            nameLabel
            removeButton
        }
        
        iconImageV.leading(16.0.Scale).size(40.0.Scale).centerVertically()
        removeButton.trailing(16.0.Scale).width(88.0.Scale).height(26.0.Scale).centerVertically()
        nameLabel.leading(68.0.Scale).width(180.0.Scale).centerVertically()
        
        removeButton.addTarget(self, action: #selector(clickRemoveButton), for: .touchUpInside)
    }
    
    @objc private func clickRemoveButton(){
        guard let userID = model?.black_id_des else { return }
        removeBlackBlock?(userID)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubBlackEmptyView: UIView {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Common_blackEmpty").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(14.font_semiBold).alignment(.center)
        .text("你还没有拉黑任何人").end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            iconImageView
            titleLabel

        }
        
        iconImageView.top(0).size(110).centerHorizontally()
        titleLabel.leading(20).trailing(20)
        titleLabel.Top == iconImageView.Bottom + 8.0.Scale
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

